<?php
/*系统自动化进程监控文件
说明：具体监控时间 请访问 /admin/set.php?act=cron 查看
监控频率建议最少5分钟一次，具体看后台介绍
监控地址：/cron.php?key=监控密钥
注意：千万不要监控太快或使用多节点监控！！！否则会导致数据错乱！
 */
if (preg_match('/Baiduspider/', $_SERVER['HTTP_USER_AGENT'])) {
    exit;
}
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
    $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0) Safari/537.36';
}

@header('Content-Type: text/html; charset=UTF-8');

include './includes/common.php';

if (empty($conf['cron_key'])) {
    exit("请先设置好监控密钥");
}

if ($conf['cron_key'] != $_GET['key']) {
    exit("监控密钥不正确");
}

if (function_exists("set_time_limit")) {
    @set_time_limit(0);
}
if (function_exists("ignore_user_abort")) {
    @ignore_user_abort(true);
}

if ($_GET['type'] == 'day' && $_GET['key'] == $conf['cron_key']) {
    $thtime = date("Y-m-d H:i:s", time() - 3600 * 24 * 7);
    $thtime2 = date("Y-m-d H:i:s", time() - 3600 * 24 * 180);
    saveSetting('cron_time1', date("Y-m-d H:i:s"));
    $sql1 = $DB->exec("delete from pre_logs where status=0 and addtime<'{$thtime}'");
    $sql2 = $DB->exec("delete from pre_logs where status=1 and addtime<'{$thtime2}'");
    $sql3 = $DB->exec("delete from pre_mails where status=0 and time<'{$thtime}'");
    $sql4 = $DB->exec("delete from pre_mails where status=1 and time<'{$thtime2}'");
    $sql5 = $DB->exec("delete from pre_mails where status=2 and time<'{$thtime}'");
    $sql6 = $DB->exec("delete from pre_mails where status>=3 and time<'{$thtime2}'");
    $thtime3 = date("Y-m-d H:i:s", time() - 3600 * 24 * 2);
    $sql7 = $DB->exec("delete from pre_orders where status=0 and addtime<'{$thtime3}'");
    $sql8 = $DB->exec("delete from pre_orders where status=1 and addtime<'{$thtime2}'");
    $DB->exec("OPTIMIZE TABLE `pre_logs`");
    $DB->exec("OPTIMIZE TABLE `pre_mails`");
    $DB->exec("OPTIMIZE TABLE `pre_orders`");
    $CACHE->clean();
    $dir = SYSTEM_ROOT . 'log/';
    if (is_dir($dir)) {
        if ($opendir = opendir($dir)) {
            while (($file = readdir($opendir)) !== false) {
                if ($file != '.' && $file != '..') {
                    if (time() - filemtime($dir . $file) > 60 * 60 * 24 * 7) {
                        unlink($dir . $file);
                    }
                }
            }
            closedir($opendir);
        }
    }
    $count = $sql1 + $sql2 + $sql3 + $sql4 + $sql5 + $sql6 + $sql7 + $sql8;
    exit('日常维护任务已成功执行，本次共清理了' . $count . '条数据');
} elseif ($_GET['type'] == 'min' && $_GET['key'] == $conf['cron_key']) {
    saveSetting('cron_time2', date("Y-m-d H:i:s"));
    auto_monitor();
    exit('ok');
} elseif ($conf['easypay_id'] && $conf['easypay_key'] && $_GET['type'] == 'pay') {
    $cron_lasttime = $conf['cron_time3'];
    if (time() - strtotime($cron_lasttime) < 30) {
        exit('ok');
    }
    saveSetting('cron_time3', $date);
    $trade_no = date("YmdHis", strtotime($cron_lasttime)) . '000';
    $limit = $DB->getColumn("SELECT count(*) FROM pre_orders WHERE trade_no>'$trade_no'");
    if ($limit < 1) {
        exit('ok');
    }

    if ($limit > 50) {
        $limit = 50;
    }

    $payapi = $conf['easypay_url'];
    if (empty($payapi)) {
        exit('未配置易支付信息');
    }

    $data = get_curl($payapi . 'api.php?act=orders&limit=' . $limit . '&pid=' . $conf['easypay_id'] . '&key=' . $conf['easypay_key']);
    $arr = json_decode($data, true);
    if ($arr['code'] == 1) {
        foreach ($arr['data'] as $row) {
            if ($row['status'] == 1) {
                $trade_no = $row['trade_no'];
                $out_trade_no = $row['out_trade_no'];
                $srow = $DB->getRow("SELECT * FROM pre_orders WHERE trade_no='{$out_trade_no}' LIMIT 1");
                if ($srow && $srow['status'] == 0) {
                    $DB->exec("UPDATE `pre_orders` SET `status`='2',`endtime`='$date',`api_trade_no`='$trade_no' WHERE `trade_no`='{$out_trade_no}'");
                    processOrder($srow);
                    echo '已成功补单:' . $out_trade_no . '<br/>';
                }
            }
        }
        exit('ok');
    } else {
        exit($arr['msg']);
    }
} else {
    exit('小可爱 你想监控什么？');
}
